package yunhe.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;

import yunhe.biz.StockMgrBiz;
import yunhe.biz.impl.StockMgrBizImpl;
import yunhe.entity.Inventory;
import yunhe.entity.Storage;
import yunhe.entity.Taking;

@WebServlet(name="GetStockProductServlet",urlPatterns="/GetStockProductServlet")
public class GetStockProductServlet  extends HttpServlet
{ 
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException
{
doPost(req,resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
	HttpSession session=req.getSession();
    String takId=(String) session.getAttribute("Takcode");
    System.out.println(takId);
    
	StockMgrBiz stock=new StockMgrBizImpl();
	//从页面获取仓库id
	String stoid=req.getParameter("reposity");
	
	//查出所有的仓库
	 List<Storage> list_storage=stock.querySto();
	 req.setAttribute("stoList", list_storage);
	 JSONArray ja=new JSONArray();
	 //若仓库id为null 则设初始值为仓库1
	 List<Taking> list1=stock.queryProByStoTakId(takId, stoid);	
    if(stoid==""||stoid==null)
    {
	 stoid="ck201606001111";
    }
    if(list1.isEmpty())
    {
  
	 List<Inventory> list=stock.searchInvBySto(stoid);
	 if(!list.isEmpty()){
		
		 for (int i=0;i<list.size();i++){
			  Map<String,String> mss=new HashMap<String,String>();
	    mss.put("proNumber",list.get(i).getProduct().getProBarCode());
        mss.put("proName",list.get(i).getProduct().getProName());
        mss.put("proBarCode",list.get(i).getProduct().getProBarCode());
        mss.put("proQual",list.get(i).getProduct().getProQual());
        mss.put("ProPurchPrice",Double.toString(list.get(i).getProduct().getProPurchPrice()));
        mss.put("proInit",list.get(i).getProduct().getProUnit());
        mss.put("preinvCount",Integer.toString(list.get(i).getInvCount()));
        ja.put(mss);
         }
	 }
         }
    else 
    { 
     //根据仓库id和盘点单号查询商品信息
    
	 if(!list1.isEmpty()){
         for (int i=0;i<list1.size();i++){
             Map<String,String> mss=new HashMap<String,String>();
             mss.put("proNumber",list1.get(i).getProduct().getProBarCode());
             mss.put("proName",list1.get(i).getProduct().getProName());
             mss.put("proBarCode",list1.get(i).getProduct().getProBarCode());
             mss.put("proQual",list1.get(i).getProduct().getProQual());
             mss.put("ProPurchPrice",Double.toString(list1.get(i).getProduct().getProPurchPrice()));
             mss.put("proInit",list1.get(i).getProduct().getProUnit());
             mss.put("invCount",Integer.toString(list1.get(i).getTakNum()));
             mss.put("preinvCount", Integer.toString(list1.get(i).getPretakNum()));
             mss.put("proLossCount", Integer.toString(list1.get(i).getPhaseNum()));
             mss.put("proLossMoney", Double.toString(list1.get(i).getPhaseDollar()));
             ja.put(mss);
         }
     }
	}
    resp.getWriter().print("{\"total\":300,\"rows\":"+ja.toString()+"}");
}
}
